#ifndef NLABOURER_H
#define NLABOURER_H
#include <QtWidgets>
#include <cmath>
#include "tools/Gadget.h"

class NLabourer : public QWidget{

public:

    typedef enum {
        QUITE,
        MAXWIN,
        MINWIN,
        NORWIN
    } Type;

    typedef Type GraphType;

    NLabourer(QWidget *parent=nullptr){
        setParent(parent);
        setProperty("graph-switch", true);
        setProperty("graph-margins", 5);
        setProperty("graph-graphcolor", QColor(Qt::white));
        setProperty("graph-graphthickness", 3);
        setProperty("graph-graphtype", Type::NORWIN);
//        setWindowOpacity(0);
    }

    QPixmap doconfigur_settting(int margins=5, int graphthickness=3, QColor graphcolor=Qt::white, GraphType graphtype = Type::NORWIN){
        setProperty("graph-switch", true);
        setProperty("graph-margins", margins);
        setProperty("graph-graphcolor", graphcolor);
        setProperty("graph-graphthickness", graphthickness);
        setProperty("graph-graphtype", graphtype);
        update();
        return graphIco;
    }


    QPixmap doconfigur_settting(int margins=5, int graphthickness=3, QColor graphcolor=Qt::white){
        setProperty("graph-switch", true);
        doconfigur_settting(margins, graphthickness, graphcolor, Type::NORWIN);
        update();
        return graphIco;
    }


    QPixmap doconfigur_settting(int margins=5, int graphthickness=3){
        setProperty("graph-switch", true);
        doconfigur_settting(margins, graphthickness, Qt::white, Type::NORWIN);
        update();
        return graphIco;
    }


    QPixmap doconfigur_settting(int margins=3){
        graphIco.fill(Qt::transparent);
        setProperty("graph-switch", true);
        doconfigur_settting(margins, 3, Qt::white, Type::NORWIN);
        update();
        return graphIco;
    }

protected:
//    pr = cx + r .cos(endAngle * Π/180)
//    py = cy - r·sin(endAngle * Π/180)

    void paintEvent(QPaintEvent*){
        bool _switch = property("graph-switch").toBool();
        if (_switch)
        {
            QPixmap pixmap = property("graph-pixmap").value<QPixmap>();
            QPainter impactPainter(&graphIco);
//            QPainter impactPainter(this);
            QRect glbrect(impactPainter.window());
            impactPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
            impactPainter.fillRect(glbrect, Qt::transparent);
            GraphType graphtype = static_cast<GraphType>(property("graph-graphtype").toUInt());
            int margin = property("graph-margins").toInt();
            QColor graphcolor = property("graph-graphcolor").value<QColor>();
            int graphthickness = property("graph-graphthickness").toInt();
            impactPainter.setPen(QPen(graphcolor, graphthickness));
            if (graphtype == GraphType::MINWIN)
            {
                int graph_xstart = margin;
                int graph_ystart = height() - graphthickness - 2;
                int graph_width = width() - margin * 2;
                impactPainter.drawLine(graph_xstart, graph_ystart, graph_xstart + graph_width, graph_ystart);
            }
            else if (graphtype == GraphType::NORWIN)
            {
                int othangle = 20;
                int angle_start = 25;
                int angle_end = 329;
                QRect roundrect(glbrect.adjusted(margin,margin,-margin *2,-margin *2));
                impactPainter.drawArc(roundrect, angle_start * 16, angle_end * 16);
                QPoint arrows_start = coord(roundrect, angle_start + othangle);
                QPoint arrows_end = coord(roundrect, angle_start);
                QPoint arrows_start_top = arrows_start - QPoint(-12, 12);
                QPoint arrows_start_down = arrows_start - QPoint(15, -12);
                impactPainter.drawLine(arrows_start_top, arrows_end);
                impactPainter.drawLine(arrows_start_down, arrows_end);
            }
            else if (graphtype == GraphType::MAXWIN)
            {
                QRect rect(glbrect.adjusted(margin,margin,-margin *2,-margin *2));
                impactPainter.drawRect(rect);
            }
            else if (graphtype == GraphType::QUITE)
            {
                int coord = sqrt(pow(margin, 2) / 2);
                QPoint linetl = QPoint(coord, coord);
                QPoint linetr = QPoint(width(), 0) - QPoint(coord, -coord);
                QPoint linebl = QPoint(0, height()) + QPoint(coord, -coord);
                QPoint linebr = QPoint(width(), height()) - QPoint(coord, coord);
                impactPainter.drawLine(linetl, linebr);
                impactPainter.drawLine(linebl, linetr);
            }
            setProperty("graph-switch", false);
        }
    }

private:
    Extend definfc;
    QPixmap graphIco;

    QPoint coord(QRect roundrect,int angle)
    {
        double radiusx = (double)roundrect.width() / 2;
        double radiusy = (double)roundrect.height() / 2;
        double round_x = roundrect.x() + radiusx;
        double round_y = roundrect.y() + radiusy;
        double radian = angle * M_PI / 180;
        double endposx = round_x + radiusx * cos(radian);
        double endposy = round_y - radiusy * sin(radian);
        return QPoint(endposx, endposy);
    }

    void resizeEvent(QResizeEvent *){
        graphIco = QPixmap(size());
        graphIco.fill(Qt::transparent);
        update();
    }

    void setVisible(bool visible){
        QWidget::setVisible(visible);

    }

    void update(){
        if (!isVisible()) setVisible(true);
        QWidget::update();
    }

};


#endif
